package org.noote.test_assistant;

import org.noote.libs.assistant.Assistant;
import org.noote.libs.assistant.Assistant_Event;
import org.noote.libs.assistant.agent.Agent_Messanger_FR;
import org.noote.libs.assistant.helper.CheckSpeechCapacities_Activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class TestAssistant_Activity extends Activity {

	protected static final int MESSAGE_UI_READY=0;
	protected static final int MESSAGE_UI_UNREADY=1;
	protected static final int MESSAGE_UI_GETERROR=2;
	protected static final int MESSAGE_UI_TOAST=3;
	protected static final int MESSAGE_UI_LOG=4;
	
	private static final int TTS_CHECK_CODE = 1;

	private Assistant _assistant;
	
	boolean _bTTS=false, _bSR=false;
	
	Agent_Messanger_FR _agent = new Agent_Messanger_FR();
	
	final Assistant_Event _events = new Assistant_Event()
	{

		@Override
		public void ttsReady() {
			_bTTS = true;
			checkReady();
			_UI_Handler.sendMessage(_UI_Handler.obtainMessage(MESSAGE_UI_LOG, "TTS ready"));
		}

		@Override
		public void ttsError(String sError) {
			_UI_Handler.sendMessage(_UI_Handler.obtainMessage(MESSAGE_UI_GETERROR, "TTS Error : " + sError));
			_UI_Handler.sendMessage(_UI_Handler.obtainMessage(MESSAGE_UI_LOG, "TTS Error : " + sError));
		}

		@Override
		public void ttsClosed() {
			if(_bTTS) unReady();
			_bTTS = false;
			_UI_Handler.sendMessage(_UI_Handler.obtainMessage(MESSAGE_UI_LOG, "TTS unready"));
		}

		@Override
		public void srReady() {
			_bSR = true;
			checkReady();
			_UI_Handler.sendMessage(_UI_Handler.obtainMessage(MESSAGE_UI_LOG, "SR ready"));
		}

		@Override
		public void srError(String sError) {
			_UI_Handler.sendMessage(_UI_Handler.obtainMessage(MESSAGE_UI_GETERROR, "SR Error : " + sError));
			_UI_Handler.sendMessage(_UI_Handler.obtainMessage(MESSAGE_UI_LOG, "SR Error : " + sError));
		}

		@Override
		public void srClosed() {
			if(_bSR) unReady();
			_bSR = false;
			_UI_Handler.sendMessage(_UI_Handler.obtainMessage(MESSAGE_UI_LOG, "SR unready"));
		}

		@Override
		public void srHeard(String sMessage)
		{
			_UI_Handler.sendMessage(_UI_Handler.obtainMessage(MESSAGE_UI_LOG, "SR heard : "+sMessage));
		}

		private void checkReady() {
			if(_bTTS && _bSR)
				_UI_Handler.sendMessage(_UI_Handler.obtainMessage(MESSAGE_UI_READY, null));
		}
		
		private void unReady() {
			_UI_Handler.sendMessage(_UI_Handler.obtainMessage(MESSAGE_UI_UNREADY, null));
		}
	};
	
	final Handler _UI_Handler = new Handler() {

	    @Override
		public void handleMessage(Message msg) {
	        switch (msg.what) {
	        case MESSAGE_UI_READY:
	        {
				Toast.makeText(getApplicationContext(), "Ready to work :)", Toast.LENGTH_SHORT).show();
				
				setReady(true);
	        }
	            break;

	        case MESSAGE_UI_UNREADY:
	        {
	        	Toast.makeText(getApplicationContext(), "Not ready :(", Toast.LENGTH_SHORT).show();

	        	setReady(false);
	        }
	            break;

	        case MESSAGE_UI_GETERROR:
	        {
	        	Toast.makeText(getApplicationContext(), "Get Error : "+(String) msg.obj, Toast.LENGTH_LONG).show();
	        }
	        	break;
	        case MESSAGE_UI_TOAST:
	        {
	        	Toast.makeText(getApplicationContext(), (String) msg.obj, Toast.LENGTH_SHORT).show();
	        }
	        	break;
	        case MESSAGE_UI_LOG:
	        {
	        	TextView textViewLog = (TextView) findViewById(R.id.textViewLog);
	        	textViewLog.setText(textViewLog.getText() + "\n" + (String) msg.obj);

	        }
	        	break;
   	        }
	    }
	};
	
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		Button buttonRecordSpeak = (Button) findViewById(R.id.buttonRecordSpeak);
		buttonRecordSpeak.setOnClickListener(new OnClickListener()
        {
			@Override
			public void onClick(View v) {
				processingSpeech();
			}
        });

		Button buttonTextProcessing = (Button) findViewById(R.id.buttonTextProcessing);
		buttonTextProcessing.setOnClickListener(new OnClickListener()
        {
			@Override
			public void onClick(View v) {
				EditText editTextToProcess = (EditText) findViewById(R.id.editTextToProcess);
				String sTextToProcess = editTextToProcess.getText().toString();

				// process text :)
				_assistant.process(sTextToProcess);
			}
        });
		
		setReady(false);
		
		Intent checkIntent = new Intent(getApplicationContext(), CheckSpeechCapacities_Activity.class);
		startActivityForResult(checkIntent, TTS_CHECK_CODE);
	}

	protected void processingSpeech() {
		Button buttonRecordSpeak = (Button) findViewById(R.id.buttonRecordSpeak);
		
		// set Assistant in listing mode :) 
		if(_assistant.isListening())
		{
			buttonRecordSpeak.setText(getApplicationContext().getString(R.string.button_record_off));
			_assistant.stopListening();
		}
		else
		{
			buttonRecordSpeak.setText(getApplicationContext().getString(R.string.button_record_on));
			_assistant.startListening();
		}
	}

	@Override
    public void onDestroy() {
        if (_assistant != null) {
            _assistant.close();
        }
        super.onDestroy();
    }
	
	@Override
    public void onResume() {
        super.onResume();        
        
		if (_assistant != null) {
			// ?
			//_assistant.startListening();
        }
    }
	@Override
    public void onPause() {
        super.onPause();
        
		if (_assistant != null) {
			if(_assistant.isListening())
			{
				_assistant.stopListening();
			}
			processingSpeech();
        }
    }

	@Override
	protected void onActivityResult(
	        int requestCode, int resultCode, Intent data) {
	    if (requestCode == TTS_CHECK_CODE) {
	    	_assistant = new Assistant(this, _events, _agent);
	    }
	}
	
    void setReady(boolean bReady)
    {
    	Button buttonRecordSpeak = (Button) findViewById(R.id.buttonRecordSpeak);
    	buttonRecordSpeak.setEnabled(bReady);
    	Button buttonTextProcessing = (Button) findViewById(R.id.buttonTextProcessing);
    	buttonTextProcessing.setEnabled(bReady);
    	EditText editTextToProcess = (EditText) findViewById(R.id.editTextToProcess);
    	editTextToProcess.setEnabled(bReady);	    	
    }
}
